package com.lianniu.mgr.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.dao.IProductModelDao;
import com.lianniu.mgr.product.model.ProductModelVo;
import com.lianniu.mgr.product.service.IProductModelService;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ProductModelServiceImpl implements IProductModelService {

	private final static Logger logger = Logger.getLogger(ProductModelServiceImpl.class);
	  
	@Resource
	IProductModelDao ProductModelDao;
	
	@Override
	public BaseResult insertProductModel(ProductModelVo curProductModelVo) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
	    logger.info("insertProductModel begin");
	    logger.info("insertProductModel: input data check begin");
	    logger.info("insertProductModel curProductModelVo:" +JSON.toJSONString(curProductModelVo));
	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curProductModelVo.getCateName())) {
		      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }	    
		if (StringUtil.isEmpty(curProductModelVo.getSortNum())) {
		      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		      return baseResult;
		    }  
	    if (StringUtil.isEmpty(curProductModelVo.getIsBottom())) {
		    	curProductModelVo.setIsBottom("0");
			  }  
			    
		if (StringUtil.isEmpty(curProductModelVo.getIsValidate())) {
		    	curProductModelVo.setIsValidate("0");
			 } 
	    logger.info("insertProductModel: input data check end");	    
	    return BaseResult.success(ProductModelDao.insertProductModel(curProductModelVo));
	}

	@Override
	public BaseResult selectByProductModelForPage(ProductModelVo curProductModelVo, PaginationInfo paginationInfo) {
		  // TODO Auto-generated method stub
	    logger.info("selectByProductModelForPage begin.");
	    if (!StringUtil.isEmpty(curProductModelVo.getCateName())) {
	    	curProductModelVo.setCateName(curProductModelVo.getCateName().replace(" ", ""));
	    }
	    logger.info("selectByProductModelForPage BrandName:" +curProductModelVo.getCateName());
	    if (!StringUtil.isEmpty(curProductModelVo.getBrandId())) {
	    	curProductModelVo.setBrandId(curProductModelVo.getBrandId().replace(" ", ""));
	    }
	    logger.info("selectByProductModelForPage brandId:" +curProductModelVo.getBrandId());
	    if (!StringUtil.isEmpty(curProductModelVo.getCateId())) {
	    	curProductModelVo.setCateId(curProductModelVo.getCateId().replace(" ", ""));
	    }
	    logger.info("selectByProductModelForPage CateId:" +curProductModelVo.getCateId());
	    
	    logger.info("selectByProductModelForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curProductModelVo.setPaginationInfo(paginationInfo);
	    List<ProductModelVo> list=ProductModelDao.selectByProductModelForPage(curProductModelVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByProductModelForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult updateById(ProductModelVo curProductModelVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curProductModel:" +JSON.toJSONString(curProductModelVo));
	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curProductModelVo.getCateName())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curProductModelVo.getSortNum())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curProductModelVo.getIsBottom())) {
	    	curProductModelVo.setIsBottom("0");
		}
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(ProductModelDao.updateById(curProductModelVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
		// TODO Auto-generated method stub
		 logger.info("deleteById begin");
		 logger.info("deleteById Id:" +Id);
		  
		 return BaseResult.success(ProductModelDao.deleteById(Id));
	}

	@Override
	public BaseResult selectById(ProductModelVo curProductModelVo) {
		 // TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curProductModelVo.getCateId());
	  
	    return BaseResult.success(ProductModelDao.selectById(curProductModelVo.getCateId()));
	}

	@Override
	public Boolean checkCateName(String cateName) {
		// TODO Auto-generated method stub
	    logger.info("checkCateName begin");
	    logger.info("checkCateName cateName:" +cateName.toString());
	    logger.info("checkCateName: input data check begin");   
	  
	    if (StringUtil.isEmpty(cateName)) {       
	      return false;
	    }
	    logger.info("checkCateName: input data check end");
	    
	    ProductModelVo productModel=new ProductModelVo();	   
	    productModel.setCateName(cateName);
	    int count=ProductModelDao.count(productModel);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

	@Override
	public BaseResult deleteProductModelBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteProductModelBatch begin");
	    logger.info("deleteProductModelBatch Ids:" +id.toString());
	    logger.info("deleteProductModelBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteProductModelBatch: input data check end");
	    
	    return BaseResult.success(ProductModelDao.deleteProductModelBatch(id));
	}

	@Override
	public BaseResult selectParentBrand(String cateId) {
		logger.info("selectParentBrand begin");
	    logger.info("selectParentBrand parm: null");
	  
	    return BaseResult.success(ProductModelDao.getProductModelKeyValue(cateId));
	}
	@Override
	public BaseResult selectProductPicByCateId(String cateId) {
		logger.info("selectProductPicByCateId begin");
	    logger.info("selectProductPicByCateId: "+cateId);
		return BaseResult.success(ProductModelDao.selectProductPicByCateId(cateId));
	}

}
